package com.juzipi.upload.utils;

import com.juzipi.demo.utils.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * @Author juzipi
 * @Date 2021/5/28 15:45
 * @Info
 */
public class FileUtils {

    public static void main(String[] args) {
        Random random = new Random();
        int i = random.nextInt(100);
        System.out.println(i);
    }


    /**
     * 保存本地路径
     * @param file 文件
     * @param path 路径
     * @return 文件
     */
    public static File saveLocal(MultipartFile file,String path){
        //新文件绝对路径
        String realPath = path + "\\" + getFileName(file);
        //校验文件大小
        if (assertAllowed(file)){
            throw new RuntimeException("文件太大");
        }
        //生成文件
        File dest = new File(realPath);
        if (!dest.getParentFile().exists()){
            //getParentFile 获取父文件？
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            return dest;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 获取文件后缀
     * @param fileName 文件名
     * @return 后缀名
     */
    public static String getSuffix(String fileName){
        return fileName.substring(fileName.lastIndexOf("."));
    }



    /**
     * 生成文件名 并加上后缀
     * @param file 文件
     * @return 新文件名
     */
    public static String getFileName(MultipartFile file){
        //获取文件名
        if (StringUtils.isNull(file)) {
            throw new RuntimeException("文件为空");
        }
        if (!assertFileName(file)){
            throw new RuntimeException("文件名过长");
        }
        String originalFilename = file.getOriginalFilename();
        Date now = new Date();
        String date = DateFormatUtils.format(now, "yyyy/MM/dd");
        //加后缀并生成新的文件名
        return  date + "/"+ UUID.randomUUID() + getSuffix(originalFilename);
    }


    /**
     * 校验文件名
     * @param file
     * @return
     */
    private static Boolean assertFileName(MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        return originalFilename.length() <= 50;
    }


    /**
     * 校验文件大小
     * @param file
     * @return
     */
    private static Boolean assertAllowed(MultipartFile file){
        long size = file.getSize();
        return size <= 50 * 1024 * 1024;
    }


    /**
     * MultipartFile 转 file
     * @param file
     * @return
     */
    public static File conversion(MultipartFile file) {
        if (file.equals("") || file.getSize() <= 0){
            throw new RuntimeException("文件为空");
        }
        try {
            InputStream ins = file.getInputStream();
            File toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
            return toFile;
        } catch (IOException e) {
            throw new RuntimeException("文件转换错误");
        }
    }

    //获取流文件
    private static void inputStreamToFile(InputStream inputStream, File file) {
        try {
            OutputStream outputStream = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[2048];
            while ((bytesRead = inputStream.read(buffer, 0, 2048)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            outputStream.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除本地临时文件
     * @param file
     */
    public static void deleteTempFile(File file) {
        if (file != null) {
            File del = new File(file.toURI());
            del.delete();
        }
    }

}
